<?php
namespace app\admin\controller;


class Bisaccount extends Base{

    public function index()
    {
        $bisaccount= model('BisAccount')->getBisAccountsByStaus(1);
        return $this->fetch('',[
            'bisaccount'=>$bisaccount,
            ]);
    }

    public function add()
    {

        if(request()->isPost()) {
            // 走插入逻辑
            $data = input('post.');
            // 严格校验提交的数据， tp5 validate 小伙伴自行完成，
            if (!empty($data['id'])){
                return $this->update($data);
            }
            $validate = validate('BisAccount');
            if(!$validate->scene('add')->check($data)) {
                $this->error($validate->getError());
            }
            $res=model('BisAccount')->get(['username' =>$data['username']]);
            if($res){
                $this->error('用户名已存在');
            }
            $data['code'] = mt_rand(100, 10000);
            $accounData = [
                'username' => $data['username'],
                'code' => $data['code'],
                'password' => md5($data['password'].$data['code']),
                'is_main' => 0, // 代表的是总管理员
                'status' => 1,
            ];


            $accountId = model('BisAccount')->save($accounData);
            if($accountId) {
                $this->success('添加成功', url('bisaccount/index'));
            }else {
                $this->error('添加失败');
            }

        }else {
            return $this->fetch('');
        }
    }

    public function edit($id)
    {
        $bisaccount=model('BisAccount')->get($id);
        return $this->fetch('',['bisaccount' =>$bisaccount]);
    }

    public function update($data)
    {
        if(empty($data['password'])){
            $res=model('BisAccount')->get(['username' =>$data['username']]);
            if($res){
                $this->error('用户名已存在');
            }
            $accountId = model('BisAccount')->save(['username'=>$data['username']],['id' =>intval($data['id'])]);
            if($accountId) {
                $this->success('更新成功', url('bisaccount/index'));
            }else {
                $this->error('更新失败');
            }
        }else{
            $validate = validate('BisAccount');
            if(!$validate->scene('update')->check($data)) {
                $this->error($validate->getError());
            }
            $res=model('BisAccount')->get(['username' =>$data['username']]);
            if($res&&$res['username']!=$data['username']){
                $this->error('用户名已存在');
            }
            if ($data['password']!=$data['repassword']){
                $this->error('两次输入密码不一致');
            }
            $data['code'] = mt_rand(100, 10000);
            $accounData = [
                'username' => $data['username'],
                'code' => $data['code'],
                'password' => md5($data['password'].$data['code']),
            ];
            $accountId = model('BisAccount')->save($accounData,['id' =>intval($data['id'])]);
            if($accountId) {
                $this->success('更新成功', url('bisaccount/index'));
            }else {
                $this->error('更新失败');
            }
        }
    }

    
    public function delist()
    {
        $bisaccount= model('BisAccount')->getBisAccountsByStaus(-1);
        return $this->fetch('',[
            'bisaccount'=>$bisaccount,
        ]);
    }
}